<?php
require_once 'ApibaseController.php';

/**
 * 不適切物件問い合わせAPI<br />
 *
 * 不適切物件問い合わせAPI
 *
 * @package apamanshop
 * @author ushijima
 * @copyright コピーライト(要確認)
 */
class Inquiry_ApiclaimController extends Inquiry_ApibaseController
{
	/**
	 * 各種問合せAPIアクション
	 *
	 * @access public
	 * @return void
	 */
	public function indexAction()
	{

		// 変数初期化処理 start

		$strCtgryCd = $this->_params['CtgryCd'];			//ｶﾃｺﾞﾘ区分
		$strSiteKbn = $this->_params['SiteKbn'];			//ｻｲﾄ区分

		$strName1 = $this->_params['NameSei'];				//名前（性）
		$strName2 = $this->_params['NameMei'];				//名前（名）
		$strMail = $this->_params['Mail'];					//ﾒｰﾙ
		$strTel = $this->_params['Tel'];					//TEL
		$strNaiyo1 = $this->_params['Inquiry1'];			//不適切内容1
		$strNaiyo2 = $this->_params['Inquiry2'];			//不適切内容2
		$strNaiyo3 = $this->_params['Inquiry3'];			//不適切内容3
		$strNaiyo4 = $this->_params['Inquiry4'];			//不適切内容4
		$strNaiyo5 = $this->_params['Inquiry5'];			//不適切内容5
		$strComment = $this->_params['Comment'];			//ｺﾒﾝﾄ
		$strOther =  $this->_params['Other'];				//その他内容

		$strTenpoCd = $this->_params['ShopCd'];				//店舗CD
		$strTenpoNm = $this->_params['TenpoNm'];			//店舗名称
		$strTenpoMail = $this->_params['TenpoMail'];		//店舗メール
		$strBukkenCd = $this->_params['BukkenCd'];			//物件CD

		//送信時刻
		$strTimeStr = $this->_params['TimeStr'];

		$add_params['TitleMail'] = "ご意見メール(お問い合わせ)";

		//--- メール判定 ---
		$this->setSendMailFlag($this->_params['Mail']);

		//各種設定処理
//		$strWork = $strWork . "【不適切物件】のお知らせ" . "\r\n";
//		$strWork = $strWork . "対象物件番号：" . $strBukkenCd . "\r\n";
//		$strWork = $strWork . "対象店舗：" . $strTenpoNm . "\r\n";
//		$strWork = $strWork . "その他掲載店舗名：" . $strAllTenpoNm . "\r\n";
		if ($strNaiyo1 != "") {
			if ($Naiyo != "") {$Naiyo = $Naiyo . ",";}
			$Naiyo = $Naiyo . $strNaiyo1;
		}
		if ($strNaiyo2 != "") {
			if ($Naiyo != "") {$Naiyo = $Naiyo . ",";}
			$Naiyo = $Naiyo . $strNaiyo2;
		}
		if ($strNaiyo3 != "") {
			if ($Naiyo != "") {$Naiyo = $Naiyo . ",";}
			$Naiyo = $Naiyo . $strNaiyo3;
		}
		if ($strNaiyo4 != "") {
			if ($Naiyo != "") {$Naiyo = $Naiyo . ",";}
			$Naiyo = $Naiyo . $strNaiyo4;
		}
		if ($strNaiyo5 != "") {
			if ($Naiyo != "") {$Naiyo = $Naiyo . ",";}
			$Naiyo = $Naiyo . $strNaiyo5 . "　[" . $strOther . "]";
		}

//		$strWork = $strWork . "不適切内容：" . $Naiyo;
//		$strWork = $strWork . "\r\n" . "（詳細）"  . " " . $strComment;

		$this->_params['Comment'] = $strComment;
		$this->_params['TargetKbn'] = "102";
		$this->_params['Name'] = trim($strName1) . "　" . trim($strName2);
		$this->_params['Tel'] = $strTel;

		$add_params['UserCd'] = $strTenpoCd;
		$add_params['ClaimTgtKbnIcon'] = "03";
		$add_params['UserSonotaCd'] = "NULL";
		$add_params['Bikou'] = "NULL";
		$add_params['Naiyo'] = $Naiyo;

		// ﾛｸﾞ情報格納処理 *****************************************
		try {
			// モデルの呼び出し
			$this->_model = new Inquiry_Model_Apiinquiry();

			//トランザクション開始
			$db = $this->_model->getDbConnection();

			//トランザクション開始
			$this->_model->startTransaction($db);

			//全ての店舗取得
			__log_debug(__FILE__, __LINE__, __METHOD__, "cgene debug : getBukkenAdjest");
			$strAllTenpoNm = $this->_model->getBukkenAdjest($db,$this->_params);	//全て店舗名称
			$firstflg = true;
			foreach($strAllTenpoNm as $key => $value){
				if($firstflg){
					$add_params['AllTenpoNm'] = $value['M05_TENPO_NAME'];
					$firstflg = false;
				}else{
					$add_params['AllTenpoNm'] = $add_params['AllTenpoNm'] . "　" . $value['M05_TENPO_NAME'];
				}
			}
			__log_debug(__FILE__, __LINE__, __METHOD__, "cgene debug : isLogIns");
			if ($this->isLogIns()) {
				//対応状況TBLﾃﾞｰﾀ挿入処理
				__log_debug(__FILE__, __LINE__, __METHOD__, "cgene debug : insTaiou");
				$resApi = $this->_model->insTaiou($db,$this->_params,$add_params);
			}

			__log_debug(__FILE__, __LINE__, __METHOD__, "cgene debug : SendMailMain");
			// ﾒｰﾙ送信処理 ********************************************
			$result =  $this->SendMailMain($strMail,$add_params);

			if($result == "0"){
				 $this->raiseError('メール送信エラー');
			}
			__log_debug(__FILE__, __LINE__, __METHOD__, "cgene debug : commitTransaction");

			__log_debug(__FILE__, __LINE__, __METHOD__, "コミットします");
			$this->_model->commitTransaction($db);
			$db->closeConnection();
			
			__log_debug(__FILE__, __LINE__, __METHOD__, "cgene debug : end");

			$result = 1;

		} catch (Exception $e) {
			__log_debug(__FILE__, __LINE__, __METHOD__, "ロールバックします");
			$this->_model->rollbackTransaction($db);
			$db->closeConnection();
			$result = 0;
		}

		// 返却データ作成
		$resResult = array('api_result' => array('result' => $result,'res_detail' => $resApi));
		// 返却処理
		$this->_api_data = $resResult;
		$this->display();

	}

	//メール送信制御
	private function SendMailMain($mail,$addinfo) {
		// ｽﾃｰﾀｽ初期化

		// ユーザへメール送信
		if ($mail != "") {
			$userResult = $this->SendMailUser($mail,$addinfo);
		}

		// 本部へのメール送信
		$officeResult = $this->SendMailOffice($addinfo);
		//$gyoumuResult = $this->SendMailGyoumu($addinfo);
		$gyoumuResult = 1;	// スパム扱いされているため一時的に業者宛メールを廃止 2013.03.19

		$result = 1;
		if ( $userResult == 0 || $officeResult == 0|| $gyoumuResult == 0) {
			$result = 0;
		}
		return $result;
	}

	// 問い合わせユーザへのメール送信
	private function SendMailUser($mail,$addinfo) {

		// from(送信元)、to（あて先)
		$strMailTo = array($mail => "");							//送信先ﾒｰﾙｱﾄﾞﾚｽ
		$strMailFrom = array("web-patrol@apamanshop.co.jp" => "");	//送信元ﾒｰﾙｱﾄﾞﾚｽ

		// メール本文に必要なパラメータ
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_SUBJECT_KEY] = "［アパマンショップ］物件情報に対するご連絡";
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = $strMailTo;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_FROM_KEY] = $strMailFrom;

		// ﾒｰﾙ送信処理
		if($this->isSendUser() == true) {
			$result = $this->sendToiawaseMail("MailClaimUser.tpl",array_merge($this->_params,$addinfo), $mail_params);
		}

		return $result;
	}

	// 本部のメール送信
	private function SendMailOffice($addinfo) {

		$strMailFrom = array("web-patrol@apamanshop.co.jp" => "");	//送信元ﾒｰﾙｱﾄﾞﾚｽ
		$strMailTo =   array("web-patrol@apamanshop.co.jp" => "");		//送信先ﾒｰﾙｱﾄﾞﾚｽ
		$strMailcc = array();
		$strMailcc[Apm_Config_Mail::MAIL_NETWORK] = "";
		$strMailcc[Apm_Config_Mail::MAIL_WEBMASTER] = "";

		//@ 送信先ﾒｰﾙｱﾄﾞﾚｽ
		// 本文の設定
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_SUBJECT_KEY] = "不適切物件がドットコムサイトより通報されました。";
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = $strMailTo;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_FROM_KEY] = $strMailFrom;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_CC_KEY] = $strMailcc;

		if($this->isSendHonbu() == true) {
			// ﾒｰﾙ送信処理
			$result = $this->sendToiawaseMail("MailClaimHonbu.tpl",array_merge($this->_params,$addinfo), $mail_params);
		}else if($this->getReplaceHonbuMail() != "") {
			$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = array($this->getReplaceHonbuMail() => "");
			$mail_params[Apm_Otheritems::SEND_MAIL_EX_CC_KEY] = array($this->getReplaceHonbuMail() => "");
			// ﾒｰﾙ送信処理
			$result = $this->sendToiawaseMail("MailClaimHonbu.tpl",array_merge($this->_params,$addinfo), $mail_params);
		}

		return $result;
	}

	// 業者へのメール送信
	private function SendMailGyoumu($addinfo) {

		// from(送信元)、to（あて先)
		$strMailTo = array("apamanshop@eope.co.jp" => "");			//送信先ﾒｰﾙｱﾄﾞﾚｽ
		$strMailFrom = array("web-patrol@apamanshop.co.jp" => "");	//送信元ﾒｰﾙｱﾄﾞﾚｽ

		// メール本文に必要なパラメータ
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_SUBJECT_KEY] = "お客様からの不適切物件情報のご連絡です。";
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = $strMailTo;
		$mail_params[Apm_Otheritems::SEND_MAIL_EX_FROM_KEY] = $strMailFrom;

		if($this->isSendHonbu() == true) {
			// ﾒｰﾙ送信処理
			$result = $this->sendToiawaseMail("MailClaimGyosya.tpl",array_merge($this->_params,$addinfo), $mail_params);
		}else if($this->getReplaceHonbuMail() != "") {
			$mail_params[Apm_Otheritems::SEND_MAIL_EX_TO_KEY] = array($this->getReplaceHonbuMail() => "");
			// ﾒｰﾙ送信処理
			$result = $this->sendToiawaseMail("MailClaimGyosya.tpl",array_merge($this->_params,$addinfo), $mail_params);
		}

		return $result;
	}
}
?>
